/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.persists;

import fait.ferm.global.Glob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alex
 */
public class Directory {
	private HashMap<Integer, SimpleDirItem> map;
//	private String table;
	
	public Directory(String pTable){
		map = new HashMap<>();
		Connection con = Glob.getConnection();
		ResultSet rs = null;
		try (Statement st = con.createStatement()) {
			String q = "SELECT id, name FROM "+pTable;
			rs = st.executeQuery(q);
			Integer id;
			String name;
			while(rs.next()){
				id = rs.getInt("id");
				name = rs.getString("name");
				map.put(id, new SimpleDirItem(id, name, pTable));
			}
		}
		catch (SQLException ex) {
			Logger.getLogger(Ferm.class.getName()).log(Level.SEVERE, null, ex);
		}finally{
			try {			
				rs.close();
			} catch (SQLException ex) {
				Logger.getLogger(Ferm.class.getName()).log(Level.SEVERE, null, ex);
			}
		}		
	}
	
	public SimpleDirItem get(Integer id){
		return map.get(id);
	}
	
	public Collection<SimpleDirItem> getItems(){
		return map.values();
	}
	
	public int size(){
		return map.size();
	}
	
}
